//
//  MainVC.swift
//  StoreProject
//
//  Created by CYC on 2021/9/28.
//

import UIKit

class MainVC: BaseViewController {

    var dataList = [YearModel]()
    var offestList = [CGFloat]()
    var scrollView = UIScrollView()
    var detailVC = DetailVC()
    
    // MARK:======================================生命周期========================================
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.titleLabel?.text = "日历"
        self.loadDataAction()
    }
    
    func creatUIAction() {
        
        if true {
            
            let btn = UIButton.init(frame: CGRect.init(x: kSpaceSize, y: kNavHeight - 44, width: 100, height: 44))
            btn.contentHorizontalAlignment = .left
            btn.setTitle("今天".languageText, for: .normal)
            btn.setTitleColor(kMainColor, for: .normal)
            btn.titleLabel?.font = kMFont(15)
            btn.addTarget(self, action: #selector(button1Action(btn:)), for: .touchUpInside)
            self.navView!.addSubview(btn)
        }
        
        if true {
            
            let btn = UIButton.init(frame: CGRect.init(x: kScreenWidth - kSpaceSize - 100, y: kNavHeight - 44, width: 100, height: 44))
            btn.contentHorizontalAlignment = .right
            btn.imageView?.contentMode = .scaleAspectFit
            btn.c_setImage("13")
            btn.addTarget(self, action: #selector(button3Action(btn:)), for: .touchUpInside)
            self.navView!.addSubview(btn)
        }
        
        let scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: kNavHeight, width: kScreenWidth, height: kScreenHeight - kNavHeight))
        scrollView.alwaysBounceVertical = true
        scrollView.alwaysBounceHorizontal = false
        scrollView.showsVerticalScrollIndicator = false
        self.view.addSubview(scrollView)
        self.scrollView = scrollView
        
        var height = CGFloat(0.0)
        
        for model1 in self.dataList {
            
            self.offestList.append(height)
            
            let view1 = YearView.init(frame: .init(x: kSpaceSize, y: height, width: kScreenWidth - kSpaceSize*2, height: 60))
            view1.creatUIAction(model: model1)
            view1.backgroundColor = .clear
            scrollView.addSubview(view1)
            
            let BW = (view1.c_width() - kContentSize*2) / 3.0
            let BH = CGFloat(30 + 20*6)
            for month in 0..<12 {
                
                let model2 = model1.monthList[month]
                let btn = MonthButton.init(frame: .init(x: view1.c_left() + (BW + kContentSize) * CGFloat(month % 3), y: view1.c_bottom() + kSpaceSize + (BH + kContentSize) * CGFloat(month / 3), width: BW, height: BH))
                btn.addTarget(self, action: #selector(button2Action(btn:)), for: .touchUpInside)
                btn.creatUIAction(model: model2)
                scrollView.addSubview(btn)
            }
            
            height = view1.c_bottom() + kSpaceSize + (BH + kContentSize)*4
            
        }
        
        scrollView.contentSize = CGSize.init(width: CGFloat(kScreenWidth), height: CGFloat(height + 50))
        
    }
    
    
    // MARK:======================================点击响应========================================
    
    // MARK:今天
    @objc func button1Action(btn: UIButton) {
    
        for index in 0..<self.dataList.count {
            
            let model = self.dataList[index]
            if model.year == kTodayCom.year {
                
                let offestY = self.offestList[index]
                self.scrollView.setContentOffset(.init(x: 0, y: offestY), animated: true)
                return
            }
        }
        
    }
    
    // MARK:点击月份
    @objc func button2Action(btn: MonthButton) {
    
        self.navigationController?.pushViewController(self.detailVC, animated: true)
        
        self.detailVC.setMonthAction(model: btn.model)
    }
    
    // MARK:点击添加
    @objc func button3Action(btn: UIButton) {
        
        self.navigationController?.pushViewController(AddNoticeVC(), animated: true)
    }
    
    // MARK:======================================网络请求========================================
    
    // MARK:获取数据
    @objc func loadDataAction() {
        
        
        let calendar = Calendar.current
        var com = DateComponents()
        
        if true {
            
            // 初始化第一天，从去年开始
            com.year = kTodayCom.year! - 1
            com.month = 1
            com.day = 1
            var tempCom = DateComponents()
            tempCom.setValue(0, for: .day)
            let tempDate = calendar.date(byAdding: tempCom, to: calendar.date(from: com)!)
            com = calendar.dateComponents([.year, .month, .day, .weekday], from: tempDate!)
        }
        
        for _ in 0..<5 {             // 2015年 ~ 2025年
            
            let yearModel = YearModel()
            yearModel.year = com.year!
            
            for _ in 0..<12 {              // 1月 ~ 12月
                
                let monthModel = MonthModel()
                monthModel.year = com.year!
                monthModel.month = com.month!
                
                for _ in 0..<31 {          // 1号 ~ 31号
                    
                    let dayModel = DayModel()
                    dayModel.day = com.day!
                    dayModel.date = com
                    monthModel.dayList.append(dayModel)
                    
//                    let text = "\(com.year!)-\(com.month!)-\(com.day!) \t\(com.weekday!)"
//                    print(text)
                    
                    // 明天
                    var nextCom = DateComponents()
                    nextCom.setValue(+1, for: .day)     // 天数+1
                    let nextDate = calendar.date(byAdding: nextCom, to: calendar.date(from: com)!)
                    com = calendar.dateComponents([.year, .month, .day, .weekday], from: nextDate!)
                    
                    if monthModel.month != com.month {
                        
                        // 下个月
                        break
                    }

                }
                yearModel.monthList.append(monthModel)
                
            }
            self.dataList.append(yearModel)
        }
        
        self.detailVC.creatUIAction(list: self.dataList)
        self.creatUIAction()
    }
    
    // MARK:======================================代理方法========================================
    
    
    
    
    
    


}
